package cn.cqone.crm.controller;

import cn.cqone.crm.domain.Permission;
import cn.cqone.crm.dto.IdsDto;
import cn.cqone.crm.query.PermissionQuery;
import cn.cqone.crm.service.PermissionService;
import cn.cqone.crm.utils.JSONResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/permission")
@CrossOrigin
@RequiresPermissions("permission:main")
public class PermissionController extends BaseController<Permission,Long,PermissionQuery>{

    @Autowired
    private PermissionService permissionService;

    //查询全部数据
    @RequestMapping(value = "/findPermission",method = RequestMethod.GET)
    public JSONResult findPermission(){
        try{
            List<Permission> list = permissionService.findPermission();
            return JSONResult.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }

    //取出当前角色的某些权限
    @RequestMapping(value = "/deleteRolePermission/{role_id}/{permission_id}",method = RequestMethod.POST)
    public JSONResult deleteRolePermission(@PathVariable("role_id") Long role_id,@PathVariable("permission_id") Long permission_id){
        try{
            permissionService.deleteRolePermission(role_id,permission_id);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }

    //给当前角色分配权限
    @RequestMapping(value = "/setRolePermission/{role_id}",method = RequestMethod.POST)
    public JSONResult setRolePermission(@RequestBody IdsDto permission_ids,@PathVariable("role_id") Long role_id){
        try{
            permissionService.addRolePermission(role_id,permission_ids);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }
}
